﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_High_level
{
    class MyGenericClass
    {
        public int Add(int num1,int num2)
        {
            return num1 + num2;
        }
        public float Add(float num1, float num2)
        {
            return num1 + num2;
        }

        /// <summary>
        /// 泛型方法
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <typeparam name="T1">第一个操作数类型</typeparam>
        /// <typeparam name="T2">第二个操作数类型</typeparam>
        /// <param name="num1"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        public T Add<T,T1,T2>(T1 num1,T2 num2) where T:struct,T1,T2
        {
            //return num1 + num2; //这个是错误的
            dynamic a = num1;
            dynamic b = num2;
            return a + b;
        }
    }
    public class MyList<T>
    {
        private List<T> list = new List<T>();

        /// <summary>
        /// 索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get { return list[index]; }
            set
            {
                list[index] = value;
            }
        }

        /// <summary>
        /// 返回元素个数
        /// </summary>
        public int Length
        {
            get { return list.Count; }
        }

        /// <summary>
        /// 添加一个元素
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            list.Add(item);
        }

        /// <summary>
        /// 添加一个数组元素
        /// </summary>
        /// <param name="item"></param>
        public void AddRange(T[] item)
        {
            list.AddRange(item);
        }
    }
}
